clear all
set maxvar 32000
set more off

global username = c(username)
global dropbox "/Users/${username}/Dropbox"
global master_file_path "${dropbox}/Engel_GFT/replication_files"

global input "$master_file_path\data/intermediate_data"

set more off

local gtools "g"
local foldno "Panel_A"


cap mkdir "${dump}/QS_V`foldno'/"  // this is just a scratch folder for temp files
capture mkdir "$input/Quasi_separability/V`foldno'/" // this is where dtas go at end

cd "$input/conventional_price_indices"
cd "${dir}"
global input_cpi "$input/conventional_price_indices"

global input_engel "$input/Engel_curves\Engel_V1"

global input_outside_prices "$master_file_path\data\source_data\QS_Outside_Prices_CPI_Data"

global output "$input/Quasi_separability/V`foldno'/"


**************************************************************************************
*(1) Create dataset with change in expenditure share at the i level over the 2 rounds:
*************************************************************************************

cd "$master_file_path\data/intermediate_data\hh_shares"

*Set this local to 1 if you want to recreate QSV4 dataset:
local create_QS=1

if `create_QS'==1 {
insheet using "$input_outside_prices\CPI_data_collated.csv", c  clear
egen price_change=rowmean(july-june)
drop july-june
*We work with the ag labor index only:
drop if type=="RURAL LABOURERS"
keep if year=="1999-2000"
gen state43=.
replace state43=2 if state=="ANDHRA PRADESH"
replace state43=3 if state=="ASSAM"
replace state43=4 if state=="BIHAR"
replace state43=5 if state=="GUJARAT"
replace state43=6 if state=="HARYANA"
replace state43=7 if state=="HIMACHAL PRADESH"
replace state43=8 if state=="JAMMU & KASHMIR"
replace state43=9 if state=="KARNATAKA"
replace state43=10 if state=="KERALA"
replace state43=11 if state=="MADHYA PRADESH"
replace state43=12 if state=="MAHARASHTRA"
replace state43=13 if state=="MANIPUR"
replace state43=14 if state=="MEGHALAYA"
replace state43=16 if state=="ORISSA"
replace state43=17 if state=="PUNJAB"
replace state43=18 if state=="RAJASTHAN"
replace state43=20 if state=="TAMIL NADU"
replace state43=21 if state=="TRIPURA"
replace state43=22 if state=="UTTARPRADESH"
replace state43=23 if state=="WESTBENGAL"

drop type base state year
label var price_change "Price change from 1986-1111987 to 1999-2000 for ag labourers"
save "$output/outside_prices_for_QS.dta", replace

use "$master_file_path\data/intermediate_data\hh_shares\hh_shares_43_55V1_DM_G108_15_rural.dta", clear

*This drops markets with less than 100 hhds in either round - our main estimation sample:
egen max_count_hh=max(count_hh), by (market_id)
drop if max_count_hh<100

egen decile_dist_bounds= xtile(logexp_cap), by(round state43 sector district43 ) nq(20)
egen decile_dist= xtile(logexp_cap) if decile_dist_bounds!=1 & decile_dist_bounds!=20, by(round state43 sector district43 ) nq(9)
drop decile_dist_bounds

*Now that households are classified by round district deciles of income, calculate mean relative shares within those groups:
drop if decile_dist==.
*no need for sector, this database only has rural obs:
drop sector

collapse (mean) rshare_* logexp_cap (first) total_wt , by(round  state43 district43 market_year_id market_id decile_dist)

*Now calculate changes over rounds:

preserve
keep if round==55
keep market_id decile_dist rshare_* logexp_cap  total_wt 
rename total_wt total_wt_55
rename logexp_cap logexp_cap55
rename rshare_* rshare55_*
save rshares55, replace
restore
keep if round==43
rename total_wt total_wt_43
rename logexp_cap logexp_cap43
rename rshare_* rshare43_*
merge 1:1 market_id decile_dist using rshares55.dta

drop if _merge!=3
drop _merge
cap erase rshares55.dta

*Now calculate delta log nominal incomes over time for each decile district:
gen dlogexp_cap=logexp_cap55-logexp_cap43

*Now reshape the file to become long district-decile-i good level:
gen market_id_decile=market_id*100+decile
reshape long rshare43_ rshare55_ , i(market_id_decile) j(good_i)
drop market_id_decile round market_year_id
order state43 district43 market_id decile_dist good_i total_wt_43 total_wt_55 rshare43_ rshare55_ logexp_cap43 logexp_cap55 dlogexp_cap

*Now calculate delta log shares over time for each decile district:
gen dlog_rshare=log(rshare55_)-log(rshare43_)

save "$output/QSV4.dta", replace


*Reorganize the file:
local i=1 
while `i'<35 {
use "$input_cpi/d_ln_p_goods_i_55_43_decile.dta", clear
keep if sector=="Rural"
keep state43 district43 decile_dist dist_wt good_i sum_demo_shares_`i' sum_pluto_shares_`i' d_ln_p_pluto_55_43_`i' d_ln_p_demo_55_43_`i'
keep if good_i==`i'
rename sum_demo_shares_`i' sum_demo_shares
rename sum_pluto_shares_`i'  sum_pluto_shares
rename d_ln_p_pluto_55_43_`i' d_ln_p_pluto_55_43
rename d_ln_p_demo_55_43_`i' d_ln_p_demo_55_43
tempfile delta_prices_`i'
save `delta_prices_`i''
local i=`i'+1
}

use `delta_prices_1', clear

local i=2
while `i'<35 {
append using `delta_prices_`i''
local i=`i'+1
}

save "$input_cpi/d_ln_p_goods_i_55_43_long.dta", replace


*Now make database "wide" so we have the prices of other goods as additional variables:


local i=1
while `i'<35 {
use "$input_cpi/d_ln_p_goods_i_55_43_long.dta",clear
keep if good_i==`i'
drop good_i dist_wt
rename sum_demo_shares sum_demo_shares_good_`i'
rename sum_pluto_shares sum_pluto_shares_good_`i'
rename d_ln_p_pluto_55_43 d_ln_p_pluto_55_43_good_`i'
rename d_ln_p_demo_55_43 d_ln_p_demo_55_43_`i'
tempfile delta_prices_`i'_w
save `delta_prices_`i'_w'
local i=`i'+1
}

use `delta_prices_1_w', clear

local i=2
while `i'<35 {
merge 1:1 state43 district43 decile_dist using `delta_prices_`i'_w', nogen

local i=`i'+1
}


merge 1:m state43 district43 decile_dist using  "$input_cpi/d_ln_p_goods_i_55_43_long.dta", nogen


save d_ln_p_goods_i_55_43_long.dta, replace



merge 1:1 state43 district43 decile_dist good_i using  "$output/QSV4.dta"
keep if _merge==3
drop _merge

*make decile variable compatible with figures: 10,20, 30,...90 th percentile:
replace decile_dist=10*decile_dist

save "$output/QSV4.dta", replace
erase "$input_cpi/d_ln_p_goods_i_55_43_long.dta"
cap erase rshares55.dta

*Now add EV and CV at the market by decile level.
use "$input_engel\Short_g25_rshare_V1_DM_G108_15_bs0.dta",clear
keep market_id decile P4355lp_owbwg25cen_9901 W4355_lnmpcew W5543_lnmpcew P5543lp_owbwg25cen_9901 r4355total_wt r5543total_wt

gen cv=W4355_lnmpcew-P4355lp_owbwg25cen_9901
gen ev=W5543_lnmpcew-P5543lp_owbwg25cen_9901

rename decile decile_dist 
merge 1:m market_id decile_dist using  "$output/QSV4.dta"
keep if _merge==3
drop _merge
order market_id state43 district43 decile_dist dist_wt good_i 
sort market_id state43 district43 good_i  decile_dist 
save "$output/QSV4.dta", replace



*Organize dataset wide so that each i good r share is in a column instead of a set of rows:

local i=1
while `i'<35 {
use "$output/QSV4.dta",clear
keep if good_i==`i'
keep market_id state43 district43 decile_dist rshare43_ rshare55_ dlog_rshare
gen drshare_`i'=rshare55_ -rshare43_ 
rename dlog_rshare dlog_rshare_`i'
tempfile rshares_`i'
save `rshares_`i''
local i=`i'+1
}

use `rshares_1', clear

local i=2
while `i'<35 {
merge 1:1 market_id state43 district43 decile_dist using `rshares_`i'', nogen
local i=`i'+1
}
tempfile rshares_all
save `rshares_all'

use "$output/QSV4.dta",clear
keep if good==1 
keep market_id state43 district43 decile_dist dist_wt total_wt_43 total_wt_55 logexp_cap43 logexp_cap55 dlogexp_cap r4355total_wt r5543total_wt W4355_lnmpcew W5543_lnmpcew cv ev P4355lp_owbwg25cen_9901 P5543lp_owbwg25cen_9901 sum_demo_shares_good_1- d_ln_p_demo_55_43_34
merge 1:1 market_id state43 district43 decile_dist using `rshares_all', nogen
save "$output/QSV4_wide.dta", replace




use "$output/outside_prices_for_QS.dta", clear
keep if good=="Clothing, bedding and footwear"
drop good
tempfile price_change_clothing
save `price_change_clothing', replace

use "$output/outside_prices_for_QS.dta", clear
keep if good=="Miscellaneous"
drop good
tempfile price_change_miscellaneous
save `price_change_miscellaneous', replace

use "$output/QSV4_wide.dta", clear
merge m:1 state43 using `price_change_clothing'
rename weight weight_clothing
rename price_change price_change_clothing
drop _merge

merge m:1 state43 using `price_change_miscellaneous'
rename weight weight_miscellaneous
rename price_change price_change_miscellaneous
drop _merge

save "$output/QSV5_wide.dta", replace
}
*End of create QS test dataset





*****************************************************************************
*PREPARE DATASET  QSV5_wide_4permutation.dta
*****************************************************************************
if 1==1{
use "$output/QSV5_wide.dta", clear


*Auxiliary variables:
set matsize 10000
replace r4355total_wt=int(r4355total_wt)

*average price change within G1: 

	*demo:
egen dlog_p_demo_G1=rowmean(d_ln_p_demo_55_43_25 d_ln_p_demo_55_43_26 d_ln_p_demo_55_43_3 d_ln_p_demo_55_43_2 d_ln_p_demo_55_43_15 d_ln_p_demo_55_43_5 d_ln_p_demo_55_43_22 d_ln_p_demo_55_43_17 d_ln_p_demo_55_43_9 d_ln_p_demo_55_43_27 d_ln_p_demo_55_43_1 d_ln_p_demo_55_43_6 d_ln_p_demo_55_43_20 d_ln_p_demo_55_43_28 d_ln_p_demo_55_43_11 d_ln_p_demo_55_43_21)

	*pluto:
egen dlog_p_pluto_G1=rowmean(d_ln_p_pluto_55_43_good_25 d_ln_p_pluto_55_43_good_26 d_ln_p_pluto_55_43_good_3 d_ln_p_pluto_55_43_good_2 d_ln_p_pluto_55_43_good_15 d_ln_p_pluto_55_43_good_5 d_ln_p_pluto_55_43_good_22 d_ln_p_pluto_55_43_good_17 d_ln_p_pluto_55_43_good_9 d_ln_p_pluto_55_43_good_27 d_ln_p_pluto_55_43_good_1 d_ln_p_pluto_55_43_good_6 d_ln_p_pluto_55_43_good_20 d_ln_p_pluto_55_43_good_28 d_ln_p_pluto_55_43_good_11 d_ln_p_pluto_55_43_good_21)


*average price change within G2:
	*demo
egen dlog_p_demo_G2=rowmean(d_ln_p_demo_55_43_32 d_ln_p_demo_55_43_10 d_ln_p_demo_55_43_33 d_ln_p_demo_55_43_31 d_ln_p_demo_55_43_16 d_ln_p_demo_55_43_34 d_ln_p_demo_55_43_12 d_ln_p_demo_55_43_30 d_ln_p_demo_55_43_18 d_ln_p_demo_55_43_23 d_ln_p_demo_55_43_29 d_ln_p_demo_55_43_24 d_ln_p_demo_55_43_19)

	*pluto
egen dlog_p_pluto_G2=rowmean(d_ln_p_pluto_55_43_good_32 d_ln_p_pluto_55_43_good_10 d_ln_p_pluto_55_43_good_33 d_ln_p_pluto_55_43_good_31 d_ln_p_pluto_55_43_good_16 d_ln_p_pluto_55_43_good_34 d_ln_p_pluto_55_43_good_12 d_ln_p_pluto_55_43_good_30 d_ln_p_pluto_55_43_good_18 d_ln_p_pluto_55_43_good_23 d_ln_p_pluto_55_43_good_29 d_ln_p_pluto_55_43_good_24 d_ln_p_pluto_55_43_good_19)


*average price change within G3:
	*demo:
egen dlog_p_G3=rowmean(d_ln_p_demo_55_43_8 d_ln_p_demo_55_43_4 d_ln_p_demo_55_43_14 d_ln_p_demo_55_43_7 d_ln_p_demo_55_43_13)

	*pluto:
egen dlog_p_pluto_G3=rowmean(d_ln_p_pluto_55_43_good_8 d_ln_p_pluto_55_43_good_4 d_ln_p_pluto_55_43_good_14 d_ln_p_pluto_55_43_good_7 d_ln_p_pluto_55_43_good_13)

*Average price change across all goods:
	*demo:
egen dlog_p_demo_G1G2G3=rowmean(d_ln_p_demo_55_43_25 d_ln_p_demo_55_43_26 d_ln_p_demo_55_43_3 d_ln_p_demo_55_43_2 d_ln_p_demo_55_43_15 d_ln_p_demo_55_43_5 d_ln_p_demo_55_43_22 d_ln_p_demo_55_43_17 d_ln_p_demo_55_43_9 d_ln_p_demo_55_43_27 d_ln_p_demo_55_43_1 d_ln_p_demo_55_43_6 d_ln_p_demo_55_43_20 d_ln_p_demo_55_43_28 d_ln_p_demo_55_43_11 d_ln_p_demo_55_43_21      d_ln_p_demo_55_43_32 d_ln_p_demo_55_43_10 d_ln_p_demo_55_43_33 d_ln_p_demo_55_43_31 d_ln_p_demo_55_43_16 d_ln_p_demo_55_43_34 d_ln_p_demo_55_43_12 d_ln_p_demo_55_43_30 d_ln_p_demo_55_43_18 d_ln_p_demo_55_43_23 d_ln_p_demo_55_43_29 d_ln_p_demo_55_43_24 d_ln_p_demo_55_43_19                    d_ln_p_demo_55_43_8 d_ln_p_demo_55_43_4 d_ln_p_demo_55_43_14 d_ln_p_demo_55_43_7 d_ln_p_demo_55_43_13)

	*pluto:
egen dlog_p_pluto_G1G2G3=rowmean(d_ln_p_pluto_55_43_good_25 d_ln_p_pluto_55_43_good_26 d_ln_p_pluto_55_43_good_3 d_ln_p_pluto_55_43_good_2 d_ln_p_pluto_55_43_good_15 d_ln_p_pluto_55_43_good_5 d_ln_p_pluto_55_43_good_22 d_ln_p_pluto_55_43_good_17 d_ln_p_pluto_55_43_good_9 d_ln_p_pluto_55_43_good_27 d_ln_p_pluto_55_43_good_1 d_ln_p_pluto_55_43_good_6 d_ln_p_pluto_55_43_good_20 d_ln_p_pluto_55_43_good_28 d_ln_p_pluto_55_43_good_11 d_ln_p_pluto_55_43_good_21      d_ln_p_pluto_55_43_good_32 d_ln_p_pluto_55_43_good_10 d_ln_p_pluto_55_43_good_33 d_ln_p_pluto_55_43_good_31 d_ln_p_pluto_55_43_good_16 d_ln_p_pluto_55_43_good_34 d_ln_p_pluto_55_43_good_12 d_ln_p_pluto_55_43_good_30 d_ln_p_pluto_55_43_good_18 d_ln_p_pluto_55_43_good_23 d_ln_p_pluto_55_43_good_29 d_ln_p_pluto_55_43_good_24 d_ln_p_pluto_55_43_good_19                    d_ln_p_pluto_55_43_good_8 d_ln_p_pluto_55_43_good_4 d_ln_p_pluto_55_43_good_14 d_ln_p_pluto_55_43_good_7 d_ln_p_pluto_55_43_good_13)


*Calculate a price index change for all i goods in G2 and G3, respectively:
	*demo:
#delimit ;
gen wdlog_p_G1=d_ln_p_demo_55_43_25*sum_demo_shares_good_25+
d_ln_p_demo_55_43_26*sum_demo_shares_good_26+
d_ln_p_demo_55_43_3*sum_demo_shares_good_3+
d_ln_p_demo_55_43_2*sum_demo_shares_good_2+
d_ln_p_demo_55_43_15*sum_demo_shares_good_15+
d_ln_p_demo_55_43_5*sum_demo_shares_good_5+
d_ln_p_demo_55_43_22*sum_demo_shares_good_22+
d_ln_p_demo_55_43_17*sum_demo_shares_good_17+
d_ln_p_demo_55_43_9*sum_demo_shares_good_9+
d_ln_p_demo_55_43_27*sum_demo_shares_good_27+
d_ln_p_demo_55_43_1*sum_demo_shares_good_1+
d_ln_p_demo_55_43_6*sum_demo_shares_good_6+
d_ln_p_demo_55_43_20*sum_demo_shares_good_20+
d_ln_p_demo_55_43_28*sum_demo_shares_good_28+
d_ln_p_demo_55_43_11*sum_demo_shares_good_11+
d_ln_p_demo_55_43_21*sum_demo_shares_good_21;


	*pluto:;
gen wdlog_p_pluto_G1=d_ln_p_pluto_55_43_good_25*sum_pluto_shares_good_25+
d_ln_p_pluto_55_43_good_26*sum_pluto_shares_good_26+
d_ln_p_pluto_55_43_good_3*sum_pluto_shares_good_3+
d_ln_p_pluto_55_43_good_2*sum_pluto_shares_good_2+
d_ln_p_pluto_55_43_good_15*sum_pluto_shares_good_15+
d_ln_p_pluto_55_43_good_5*sum_pluto_shares_good_5+
d_ln_p_pluto_55_43_good_22*sum_pluto_shares_good_22+
d_ln_p_pluto_55_43_good_17*sum_pluto_shares_good_17+
d_ln_p_pluto_55_43_good_9*sum_pluto_shares_good_9+
d_ln_p_pluto_55_43_good_27*sum_pluto_shares_good_27+
d_ln_p_pluto_55_43_good_1*sum_pluto_shares_good_1+
d_ln_p_pluto_55_43_good_6*sum_pluto_shares_good_6+
d_ln_p_pluto_55_43_good_20*sum_pluto_shares_good_20+
d_ln_p_pluto_55_43_good_28*sum_pluto_shares_good_28+
d_ln_p_pluto_55_43_good_11*sum_pluto_shares_good_11+
d_ln_p_pluto_55_43_good_21*sum_pluto_shares_good_21;


	*demo:;
gen wdlog_p_G2=d_ln_p_demo_55_43_32*sum_demo_shares_good_32+
d_ln_p_demo_55_43_10*sum_demo_shares_good_10+
d_ln_p_demo_55_43_33*sum_demo_shares_good_33+
d_ln_p_demo_55_43_31*sum_demo_shares_good_31+
d_ln_p_demo_55_43_16*sum_demo_shares_good_16+
d_ln_p_demo_55_43_34*sum_demo_shares_good_34+
d_ln_p_demo_55_43_12*sum_demo_shares_good_12+
d_ln_p_demo_55_43_30*sum_demo_shares_good_30+
d_ln_p_demo_55_43_18*sum_demo_shares_good_18+
d_ln_p_demo_55_43_23*sum_demo_shares_good_23+
d_ln_p_demo_55_43_29*sum_demo_shares_good_29+
d_ln_p_demo_55_43_24*sum_demo_shares_good_24+
d_ln_p_demo_55_43_19*sum_demo_shares_good_19;

	*pluto;
gen wdlog_p_pluto_G2=d_ln_p_pluto_55_43_good_32*sum_demo_shares_good_32+
d_ln_p_pluto_55_43_good_10*sum_demo_shares_good_10+
d_ln_p_pluto_55_43_good_33*sum_demo_shares_good_33+
d_ln_p_pluto_55_43_good_31*sum_demo_shares_good_31+
d_ln_p_pluto_55_43_good_16*sum_demo_shares_good_16+
d_ln_p_pluto_55_43_good_34*sum_demo_shares_good_34+
d_ln_p_pluto_55_43_good_12*sum_demo_shares_good_12+
d_ln_p_pluto_55_43_good_30*sum_demo_shares_good_30+
d_ln_p_pluto_55_43_good_18*sum_demo_shares_good_18+
d_ln_p_pluto_55_43_good_23*sum_demo_shares_good_23+
d_ln_p_pluto_55_43_good_29*sum_demo_shares_good_29+
d_ln_p_pluto_55_43_good_24*sum_demo_shares_good_24+
d_ln_p_pluto_55_43_good_19*sum_demo_shares_good_19;


	*demo:;
gen wdlog_p_G3=d_ln_p_demo_55_43_8*sum_demo_shares_good_8+
d_ln_p_demo_55_43_4*sum_demo_shares_good_4+
d_ln_p_demo_55_43_14*sum_demo_shares_good_14+
d_ln_p_demo_55_43_7*sum_demo_shares_good_7+
d_ln_p_demo_55_43_13*sum_demo_shares_good_13;

	*pluto;
gen wdlog_p_pluto_G3=d_ln_p_pluto_55_43_good_8*sum_pluto_shares_good_8+
d_ln_p_pluto_55_43_good_4*sum_pluto_shares_good_4+
d_ln_p_pluto_55_43_good_14*sum_pluto_shares_good_14+
d_ln_p_pluto_55_43_good_7*sum_pluto_shares_good_7+
d_ln_p_pluto_55_43_good_13*sum_pluto_shares_good_13;


	*Demo all goods (G1, G2 and G3);
gen wdlog_p_demo_G1G2G3=
d_ln_p_demo_55_43_25*sum_demo_shares_good_25+
d_ln_p_demo_55_43_26*sum_demo_shares_good_26+
d_ln_p_demo_55_43_3*sum_demo_shares_good_3+
d_ln_p_demo_55_43_2*sum_demo_shares_good_2+
d_ln_p_demo_55_43_15*sum_demo_shares_good_15+
d_ln_p_demo_55_43_5*sum_demo_shares_good_5+
d_ln_p_demo_55_43_22*sum_demo_shares_good_22+
d_ln_p_demo_55_43_17*sum_demo_shares_good_17+
d_ln_p_demo_55_43_9*sum_demo_shares_good_9+
d_ln_p_demo_55_43_27*sum_demo_shares_good_27+
d_ln_p_demo_55_43_1*sum_demo_shares_good_1+
d_ln_p_demo_55_43_6*sum_demo_shares_good_6+
d_ln_p_demo_55_43_20*sum_demo_shares_good_20+
d_ln_p_demo_55_43_28*sum_demo_shares_good_28+
d_ln_p_demo_55_43_11*sum_demo_shares_good_11+
d_ln_p_demo_55_43_21*sum_demo_shares_good_21+
d_ln_p_demo_55_43_32*sum_demo_shares_good_32+
d_ln_p_demo_55_43_10*sum_demo_shares_good_10+
d_ln_p_demo_55_43_33*sum_demo_shares_good_33+
d_ln_p_demo_55_43_31*sum_demo_shares_good_31+
d_ln_p_demo_55_43_16*sum_demo_shares_good_16+
d_ln_p_demo_55_43_34*sum_demo_shares_good_34+
d_ln_p_demo_55_43_12*sum_demo_shares_good_12+
d_ln_p_demo_55_43_30*sum_demo_shares_good_30+
d_ln_p_demo_55_43_18*sum_demo_shares_good_18+
d_ln_p_demo_55_43_23*sum_demo_shares_good_23+
d_ln_p_demo_55_43_29*sum_demo_shares_good_29+
d_ln_p_demo_55_43_24*sum_demo_shares_good_24+
d_ln_p_demo_55_43_19*sum_demo_shares_good_19+
d_ln_p_demo_55_43_8*sum_demo_shares_good_8+
d_ln_p_demo_55_43_4*sum_demo_shares_good_4+
d_ln_p_demo_55_43_14*sum_demo_shares_good_14+
d_ln_p_demo_55_43_7*sum_demo_shares_good_7+
d_ln_p_demo_55_43_13*sum_demo_shares_good_13;


	*pluto all goods;
gen wdlog_p_pluto_G1G2G3=
d_ln_p_pluto_55_43_good_25*sum_pluto_shares_good_25+
d_ln_p_pluto_55_43_good_26*sum_pluto_shares_good_26+
d_ln_p_pluto_55_43_good_3*sum_pluto_shares_good_3+
d_ln_p_pluto_55_43_good_2*sum_pluto_shares_good_2+
d_ln_p_pluto_55_43_good_15*sum_pluto_shares_good_15+
d_ln_p_pluto_55_43_good_5*sum_pluto_shares_good_5+
d_ln_p_pluto_55_43_good_22*sum_pluto_shares_good_22+
d_ln_p_pluto_55_43_good_17*sum_pluto_shares_good_17+
d_ln_p_pluto_55_43_good_9*sum_pluto_shares_good_9+
d_ln_p_pluto_55_43_good_27*sum_pluto_shares_good_27+
d_ln_p_pluto_55_43_good_1*sum_pluto_shares_good_1+
d_ln_p_pluto_55_43_good_6*sum_pluto_shares_good_6+
d_ln_p_pluto_55_43_good_20*sum_pluto_shares_good_20+
d_ln_p_pluto_55_43_good_28*sum_pluto_shares_good_28+
d_ln_p_pluto_55_43_good_11*sum_pluto_shares_good_11+
d_ln_p_pluto_55_43_good_21*sum_pluto_shares_good_21+
d_ln_p_pluto_55_43_good_32*sum_pluto_shares_good_32+
d_ln_p_pluto_55_43_good_10*sum_pluto_shares_good_10+
d_ln_p_pluto_55_43_good_33*sum_pluto_shares_good_33+
d_ln_p_pluto_55_43_good_31*sum_pluto_shares_good_31+
d_ln_p_pluto_55_43_good_16*sum_pluto_shares_good_16+
d_ln_p_pluto_55_43_good_34*sum_pluto_shares_good_34+
d_ln_p_pluto_55_43_good_12*sum_pluto_shares_good_12+
d_ln_p_pluto_55_43_good_30*sum_pluto_shares_good_30+
d_ln_p_pluto_55_43_good_18*sum_pluto_shares_good_18+
d_ln_p_pluto_55_43_good_23*sum_pluto_shares_good_23+
d_ln_p_pluto_55_43_good_29*sum_pluto_shares_good_29+
d_ln_p_pluto_55_43_good_24*sum_pluto_shares_good_24+
d_ln_p_pluto_55_43_good_19*sum_pluto_shares_good_19+
d_ln_p_pluto_55_43_good_8*sum_pluto_shares_good_8+
d_ln_p_pluto_55_43_good_4*sum_pluto_shares_good_4+
d_ln_p_pluto_55_43_good_14*sum_pluto_shares_good_14+
d_ln_p_pluto_55_43_good_7*sum_pluto_shares_good_7+
d_ln_p_pluto_55_43_good_13*sum_pluto_shares_good_13;


#delimit cr;



*calculate price deviations from mean price change, square and cubic terms too:

foreach i of numlist 1/34 {
 
*NOTE: Just for block H, K:	
gen d_ln_p_demo_55_43_G1_`i'= d_ln_p_demo_55_43_`i'
*Otherwise use this one:
*demo;
*gen d_ln_p_demo_55_43_G1_`i'= d_ln_p_demo_55_43_`i' - dlog_p_demo_G1

gen d_ln_p_demo_55_43_G1_sq_`i'=d_ln_p_demo_55_43_G1_`i'^2
gen d_ln_p_demo_55_43_G1_cu_`i'=d_ln_p_demo_55_43_G1_`i'^3

*pluto;
*gen d_ln_p_pluto_55_43_G1_`i'= d_ln_p_pluto_55_43_good_`i' - dlog_p_pluto_G1
gen d_ln_p_pluto_55_43_G1_`i'= d_ln_p_pluto_55_43_good_`i' 
gen d_ln_p_pluto_55_43_G1_sq_`i'=d_ln_p_pluto_55_43_G1_`i'^2
gen d_ln_p_pluto_55_43_G1_cu_`i'=d_ln_p_pluto_55_43_G1_`i'^3

}



*Create polynomials of welfare terms:
gen ev2=ev^2
gen ev3=ev^3
gen cv2=cv^2
gen cv3=cv^3
gen dlogexp_cap2=dlogexp_cap^2
gen dlogexp_cap3=dlogexp_cap^3

*Calculate d log price change for new outside goods micellaneous and clothing:
gen dlog_p_miscellaneous=log(price_change_miscellaneous)
gen dlog_p_clothing=log(price_change_clothing)
gen dlog_p_miscellaneous_G4=dlog_p_miscellaneous-dlog_p_demo_G1
gen dlog_p_clothing_G4=dlog_p_clothing-dlog_p_demo_G1


save "$output/QSV5_wide_4permutation.dta", replace
}




if 1==1{
	*Calculate empirical mean and sd of change in log prices from india's CPI (recall these are at the state level):
use "$output/QSV5_wide_4permutation.dta", clear
duplicates drop state43, force
sum dlog_p_miscellaneous,d
global m_misc=r(mean)
global sd_misc=r(sd)

sum dlog_p_clothing,d
global m_cloth=r(mean)
global sd_cloth=r(sd)

global obs=r(N)

	*Now create a new dataset of prices with same mean and sd as in the empirical distribution for permutation tests:

	keep state43
	*This is just a column with the state43 variable:
	save ${dump}/QS_V`foldno'/starter.dta, replace
	

	forvalues i=1(1)500 {
	set seed `i'
	use ${dump}/QS_V`foldno'/starter.dta, clear
	gen dlog_p_misc_fake=rnormal($m_misc, $sd_misc) 
	gen dlog_p_cloth_fake=rnormal($m_cloth, $sd_cloth)
	save ${dump}/QS_V`foldno'/fake_prices_`i'.dta,replace
	}
}	



*Next run a loop of regressions:

if 1==1{
tempname permutation_results_V`foldno'
postfile `permutation_results_V`foldno''  str20 type run chi2 df double p N using "$output/permutation_resultsV`foldno'.dta", replace

local runs=500

forvalues r=1(1)`runs' {
use "$output/QSV5_wide_4permutation.dta", clear
*Define locals:
if 1==1{
*LOCAL -Change in log prices:
local G1_goods_d_ln_p ="d_ln_p_demo_55_43_25 d_ln_p_demo_55_43_26 d_ln_p_demo_55_43_3  d_ln_p_demo_55_43_2 d_ln_p_demo_55_43_15 d_ln_p_demo_55_43_5 d_ln_p_demo_55_43_22 d_ln_p_demo_55_43_17 d_ln_p_demo_55_43_9 d_ln_p_demo_55_43_27 d_ln_p_demo_55_43_1 d_ln_p_demo_55_43_6 d_ln_p_demo_55_43_20 d_ln_p_demo_55_43_28 d_ln_p_demo_55_43_11 d_ln_p_demo_55_43_21"

local G2_goods_d_ln_p ="d_ln_p_demo_55_43_32 d_ln_p_demo_55_43_10  d_ln_p_demo_55_43_33 d_ln_p_demo_55_43_31 d_ln_p_demo_55_43_16 d_ln_p_demo_55_43_34 d_ln_p_demo_55_43_12 d_ln_p_demo_55_43_30 d_ln_p_demo_55_43_18 d_ln_p_demo_55_43_23 d_ln_p_demo_55_43_29 d_ln_p_demo_55_43_24 d_ln_p_demo_55_43_19"

local G3_goods_d_ln_p ="d_ln_p_demo_55_43_8 d_ln_p_demo_55_43_4 d_ln_p_demo_55_43_14  d_ln_p_demo_55_43_7 d_ln_p_demo_55_43_13"


local G3_goods_d_ln_p_pluto ="d_ln_p_pluto_55_43_good_8 d_ln_p_pluto_55_43_good_4 d_ln_p_pluto_55_43_good_14  d_ln_p_pluto_55_43_good_7 d_ln_p_pluto_55_43_good_13"
local G2_goods_d_ln_p_pluto ="d_ln_p_pluto_55_43_good_32 d_ln_p_pluto_55_43_good_10  d_ln_p_pluto_55_43_good_33 d_ln_p_pluto_55_43_good_31 d_ln_p_pluto_55_43_good_16 d_ln_p_pluto_55_43_good_34 d_ln_p_pluto_55_43_good_12 d_ln_p_pluto_55_43_good_30 d_ln_p_pluto_55_43_good_18 d_ln_p_pluto_55_43_good_23 d_ln_p_pluto_55_43_good_29 d_ln_p_pluto_55_43_good_24 d_ln_p_pluto_55_43_good_19"


local welfare_change ="ev cv dlogexp_cap"
local welfare_change_poly="ev cv dlogexp_cap ev2 cv2 dlogexp_cap2 ev3 cv3 dlogexp_cap3" 

*Change in relative log prices locals:
	*demo G1:
local d_ln_p_demo_55_43_G1_linear="d_ln_p_demo_55_43_G1_25 d_ln_p_demo_55_43_G1_26 d_ln_p_demo_55_43_G1_3 d_ln_p_demo_55_43_G1_2 d_ln_p_demo_55_43_G1_15 d_ln_p_demo_55_43_G1_5 d_ln_p_demo_55_43_G1_22 d_ln_p_demo_55_43_G1_17 d_ln_p_demo_55_43_G1_9 d_ln_p_demo_55_43_G1_27 d_ln_p_demo_55_43_G1_1 d_ln_p_demo_55_43_G1_6 d_ln_p_demo_55_43_G1_20 d_ln_p_demo_55_43_G1_28 d_ln_p_demo_55_43_G1_11 d_ln_p_demo_55_43_G1_21"

	*demo G2:
local d_ln_p_demo_55_43_G2_linear="d_ln_p_demo_55_43_G1_32 d_ln_p_demo_55_43_G1_10 d_ln_p_demo_55_43_G1_33 d_ln_p_demo_55_43_G1_31 d_ln_p_demo_55_43_G1_16 d_ln_p_demo_55_43_G1_34 d_ln_p_demo_55_43_G1_12 d_ln_p_demo_55_43_G1_30 d_ln_p_demo_55_43_G1_18 d_ln_p_demo_55_43_G1_23 d_ln_p_demo_55_43_G1_29 d_ln_p_demo_55_43_G1_24 d_ln_p_demo_55_43_G1_19"

	*demo G3:
local d_ln_p_demo_55_43_G3_linear="d_ln_p_demo_55_43_G1_8 d_ln_p_demo_55_43_G1_4 d_ln_p_demo_55_43_G1_14 d_ln_p_demo_55_43_G1_7 d_ln_p_demo_55_43_G1_13 "


	*pluto:
local d_ln_p_pluto_55_43_G1_linear="d_ln_p_pluto_55_43_G1_25 d_ln_p_pluto_55_43_G1_26 d_ln_p_pluto_55_43_G1_3 d_ln_p_pluto_55_43_G1_2 d_ln_p_pluto_55_43_G1_15 d_ln_p_pluto_55_43_G1_5 d_ln_p_pluto_55_43_G1_22 d_ln_p_pluto_55_43_G1_17 d_ln_p_pluto_55_43_G1_9 d_ln_p_pluto_55_43_G1_27 d_ln_p_pluto_55_43_G1_1 d_ln_p_pluto_55_43_G1_6 d_ln_p_pluto_55_43_G1_20 d_ln_p_pluto_55_43_G1_28 d_ln_p_pluto_55_43_G1_11 d_ln_p_pluto_55_43_G1_21"

*Change in relative log prices locals including quadratic terms :
local d_ln_p_demo_55_43_G1_squared="d_ln_p_demo_55_43_G1_25 d_ln_p_demo_55_43_G1_26 d_ln_p_demo_55_43_G1_3 d_ln_p_demo_55_43_G1_2 d_ln_p_demo_55_43_G1_15 d_ln_p_demo_55_43_G1_5 d_ln_p_demo_55_43_G1_22 d_ln_p_demo_55_43_G1_17 d_ln_p_demo_55_43_G1_9 d_ln_p_demo_55_43_G1_27 d_ln_p_demo_55_43_G1_1 d_ln_p_demo_55_43_G1_6 d_ln_p_demo_55_43_G1_20 d_ln_p_demo_55_43_G1_28 d_ln_p_demo_55_43_G1_11 d_ln_p_demo_55_43_G1_21      d_ln_p_demo_55_43_G1_sq_25 d_ln_p_demo_55_43_G1_sq_26 d_ln_p_demo_55_43_G1_sq_3 d_ln_p_demo_55_43_G1_sq_2 d_ln_p_demo_55_43_G1_sq_15 d_ln_p_demo_55_43_G1_sq_5 d_ln_p_demo_55_43_G1_sq_22 d_ln_p_demo_55_43_G1_sq_17 d_ln_p_demo_55_43_G1_sq_9 d_ln_p_demo_55_43_G1_sq_27 d_ln_p_demo_55_43_G1_sq_1 d_ln_p_demo_55_43_G1_sq_6 d_ln_p_demo_55_43_G1_sq_20 d_ln_p_demo_55_43_G1_sq_28 d_ln_p_demo_55_43_G1_sq_11 d_ln_p_demo_55_43_G1_sq_21"

	*pluto:
local d_ln_p_pluto_55_43_G1_squared="d_ln_p_pluto_55_43_G1_25 d_ln_p_pluto_55_43_G1_26 d_ln_p_pluto_55_43_G1_3 d_ln_p_pluto_55_43_G1_2 d_ln_p_pluto_55_43_G1_15 d_ln_p_pluto_55_43_G1_5 d_ln_p_pluto_55_43_G1_22 d_ln_p_pluto_55_43_G1_17 d_ln_p_pluto_55_43_G1_9 d_ln_p_pluto_55_43_G1_27 d_ln_p_pluto_55_43_G1_1 d_ln_p_pluto_55_43_G1_6 d_ln_p_pluto_55_43_G1_20 d_ln_p_pluto_55_43_G1_28 d_ln_p_pluto_55_43_G1_11 d_ln_p_pluto_55_43_G1_21      d_ln_p_pluto_55_43_G1_sq_25 d_ln_p_pluto_55_43_G1_sq_26 d_ln_p_pluto_55_43_G1_sq_3 d_ln_p_pluto_55_43_G1_sq_2 d_ln_p_pluto_55_43_G1_sq_15 d_ln_p_pluto_55_43_G1_sq_5 d_ln_p_pluto_55_43_G1_sq_22 d_ln_p_pluto_55_43_G1_sq_17 d_ln_p_pluto_55_43_G1_sq_9 d_ln_p_pluto_55_43_G1_sq_27 d_ln_p_pluto_55_43_G1_sq_1 d_ln_p_pluto_55_43_G1_sq_6 d_ln_p_pluto_55_43_G1_sq_20 d_ln_p_pluto_55_43_G1_sq_28 d_ln_p_pluto_55_43_G1_sq_11 d_ln_p_pluto_55_43_G1_sq_21 "

	*demo G2:
local d_ln_p_demo_55_43_G2_squared="d_ln_p_demo_55_43_G1_32 d_ln_p_demo_55_43_G1_10 d_ln_p_demo_55_43_G1_33 d_ln_p_demo_55_43_G1_31 d_ln_p_demo_55_43_G1_16 d_ln_p_demo_55_43_G1_34 d_ln_p_demo_55_43_G1_12 d_ln_p_demo_55_43_G1_30 d_ln_p_demo_55_43_G1_18 d_ln_p_demo_55_43_G1_23 d_ln_p_demo_55_43_G1_29 d_ln_p_demo_55_43_G1_24 d_ln_p_demo_55_43_G1_19      d_ln_p_demo_55_43_G1_sq_32 d_ln_p_demo_55_43_G1_sq_10 d_ln_p_demo_55_43_G1_sq_33 d_ln_p_demo_55_43_G1_sq_31 d_ln_p_demo_55_43_G1_sq_16 d_ln_p_demo_55_43_G1_sq_34 d_ln_p_demo_55_43_G1_sq_12 d_ln_p_demo_55_43_G1_sq_30 d_ln_p_demo_55_43_G1_sq_18 d_ln_p_demo_55_43_G1_sq_23 d_ln_p_demo_55_43_G1_sq_29 d_ln_p_demo_55_43_G1_sq_24 d_ln_p_demo_55_43_G1_sq_19 "

	*pluto G2:
local d_ln_p_pluto_55_43_G2_squared="d_ln_p_pluto_55_43_G1_32 d_ln_p_pluto_55_43_G1_10 d_ln_p_pluto_55_43_G1_33 d_ln_p_pluto_55_43_G1_31 d_ln_p_pluto_55_43_G1_16 d_ln_p_pluto_55_43_G1_34 d_ln_p_pluto_55_43_G1_12 d_ln_p_pluto_55_43_G1_30 d_ln_p_pluto_55_43_G1_18 d_ln_p_pluto_55_43_G1_23 d_ln_p_pluto_55_43_G1_29 d_ln_p_pluto_55_43_G1_24 d_ln_p_pluto_55_43_G1_19      d_ln_p_pluto_55_43_G1_sq_32 d_ln_p_pluto_55_43_G1_sq_10 d_ln_p_pluto_55_43_G1_sq_33 d_ln_p_pluto_55_43_G1_sq_31 d_ln_p_pluto_55_43_G1_sq_16 d_ln_p_pluto_55_43_G1_sq_34 d_ln_p_pluto_55_43_G1_sq_12 d_ln_p_pluto_55_43_G1_sq_30 d_ln_p_pluto_55_43_G1_sq_18 d_ln_p_pluto_55_43_G1_sq_23 d_ln_p_pluto_55_43_G1_sq_29 d_ln_p_pluto_55_43_G1_sq_24 d_ln_p_pluto_55_43_G1_sq_19 "

	*demo G3:
local d_ln_p_demo_55_43_G2_squared="d_ln_p_demo_55_43_G1_8 d_ln_p_demo_55_43_G1_4 d_ln_p_demo_55_43_G1_14 d_ln_p_demo_55_43_G1_7 d_ln_p_demo_55_43_G1_13       d_ln_p_demo_55_43_G1_sq_8 d_ln_p_demo_55_43_G1_sq_4 d_ln_p_demo_55_43_G1_sq_14 d_ln_p_demo_55_43_G1_sq_7 d_ln_p_demo_55_43_G1_sq_13    "

	*pluto G3:
local d_ln_p_pluto_55_43_G3_squared="d_ln_p_pluto_55_43_G1_8 d_ln_p_pluto_55_43_G1_4 d_ln_p_pluto_55_43_G1_14 d_ln_p_pluto_55_43_G1_7 d_ln_p_pluto_55_43_G1_13       d_ln_p_pluto_55_43_G1_sq_8 d_ln_p_pluto_55_43_G1_sq_4 d_ln_p_pluto_55_43_G1_sq_14 d_ln_p_pluto_55_43_G1_sq_7 d_ln_p_pluto_55_43_G1_sq_13  "



*Change in relative log prices locals including quadratic and cubic terms (notice I drop one of the goods - the last one):
	*demo:
local d_ln_p_demo_55_43_G1_poly="d_ln_p_demo_55_43_G1_25 d_ln_p_demo_55_43_G1_26 d_ln_p_demo_55_43_G1_3 d_ln_p_demo_55_43_G1_2 d_ln_p_demo_55_43_G1_15 d_ln_p_demo_55_43_G1_5 d_ln_p_demo_55_43_G1_22 d_ln_p_demo_55_43_G1_17 d_ln_p_demo_55_43_G1_9 d_ln_p_demo_55_43_G1_27 d_ln_p_demo_55_43_G1_1 d_ln_p_demo_55_43_G1_6 d_ln_p_demo_55_43_G1_20 d_ln_p_demo_55_43_G1_28 d_ln_p_demo_55_43_G1_11 d_ln_p_demo_55_43_G1_21      d_ln_p_demo_55_43_G1_sq_25 d_ln_p_demo_55_43_G1_sq_26 d_ln_p_demo_55_43_G1_sq_3 d_ln_p_demo_55_43_G1_sq_2 d_ln_p_demo_55_43_G1_sq_15 d_ln_p_demo_55_43_G1_sq_5 d_ln_p_demo_55_43_G1_sq_22 d_ln_p_demo_55_43_G1_sq_17 d_ln_p_demo_55_43_G1_sq_9 d_ln_p_demo_55_43_G1_sq_27 d_ln_p_demo_55_43_G1_sq_1 d_ln_p_demo_55_43_G1_sq_6 d_ln_p_demo_55_43_G1_sq_20 d_ln_p_demo_55_43_G1_sq_28 d_ln_p_demo_55_43_G1_sq_11 d_ln_p_demo_55_43_G1_sq_21       d_ln_p_demo_55_43_G1_cu_25 d_ln_p_demo_55_43_G1_cu_26 d_ln_p_demo_55_43_G1_cu_3 d_ln_p_demo_55_43_G1_cu_2 d_ln_p_demo_55_43_G1_cu_15 d_ln_p_demo_55_43_G1_cu_5 d_ln_p_demo_55_43_G1_cu_22 d_ln_p_demo_55_43_G1_cu_17 d_ln_p_demo_55_43_G1_cu_9 d_ln_p_demo_55_43_G1_cu_27 d_ln_p_demo_55_43_G1_cu_1 d_ln_p_demo_55_43_G1_cu_6 d_ln_p_demo_55_43_G1_cu_20 d_ln_p_demo_55_43_G1_cu_28 d_ln_p_demo_55_43_G1_cu_11 d_ln_p_demo_55_43_G1_cu_21"

	*pluto:
local d_ln_p_pluto_55_43_G1_poly="d_ln_p_pluto_55_43_G1_25 d_ln_p_pluto_55_43_G1_26 d_ln_p_pluto_55_43_G1_3 d_ln_p_pluto_55_43_G1_2 d_ln_p_pluto_55_43_G1_15 d_ln_p_pluto_55_43_G1_5 d_ln_p_pluto_55_43_G1_22 d_ln_p_pluto_55_43_G1_17 d_ln_p_pluto_55_43_G1_9 d_ln_p_pluto_55_43_G1_27 d_ln_p_pluto_55_43_G1_1 d_ln_p_pluto_55_43_G1_6 d_ln_p_pluto_55_43_G1_20 d_ln_p_pluto_55_43_G1_28 d_ln_p_pluto_55_43_G1_11 d_ln_p_pluto_55_43_G1_21      d_ln_p_pluto_55_43_G1_sq_25 d_ln_p_pluto_55_43_G1_sq_26 d_ln_p_pluto_55_43_G1_sq_3 d_ln_p_pluto_55_43_G1_sq_2 d_ln_p_pluto_55_43_G1_sq_15 d_ln_p_pluto_55_43_G1_sq_5 d_ln_p_pluto_55_43_G1_sq_22 d_ln_p_pluto_55_43_G1_sq_17 d_ln_p_pluto_55_43_G1_sq_9 d_ln_p_pluto_55_43_G1_sq_27 d_ln_p_pluto_55_43_G1_sq_1 d_ln_p_pluto_55_43_G1_sq_6 d_ln_p_pluto_55_43_G1_sq_20 d_ln_p_pluto_55_43_G1_sq_28 d_ln_p_pluto_55_43_G1_sq_11 d_ln_p_pluto_55_43_G1_sq_21       d_ln_p_pluto_55_43_G1_cu_25 d_ln_p_pluto_55_43_G1_cu_26 d_ln_p_pluto_55_43_G1_cu_3 d_ln_p_pluto_55_43_G1_cu_2 d_ln_p_pluto_55_43_G1_cu_15 d_ln_p_pluto_55_43_G1_cu_5 d_ln_p_pluto_55_43_G1_cu_22 d_ln_p_pluto_55_43_G1_cu_17 d_ln_p_pluto_55_43_G1_cu_9 d_ln_p_pluto_55_43_G1_cu_27 d_ln_p_pluto_55_43_G1_cu_1 d_ln_p_pluto_55_43_G1_cu_6 d_ln_p_pluto_55_43_G1_cu_20 d_ln_p_pluto_55_43_G1_cu_28 d_ln_p_pluto_55_43_G1_cu_11 d_ln_p_pluto_55_43_G1_cu_21"

	*demo G2:
local d_ln_p_demo_55_43_G2_poly="d_ln_p_demo_55_43_G1_32 d_ln_p_demo_55_43_G1_10 d_ln_p_demo_55_43_G1_33 d_ln_p_demo_55_43_G1_31 d_ln_p_demo_55_43_G1_16 d_ln_p_demo_55_43_G1_34 d_ln_p_demo_55_43_G1_12 d_ln_p_demo_55_43_G1_30 d_ln_p_demo_55_43_G1_18 d_ln_p_demo_55_43_G1_23 d_ln_p_demo_55_43_G1_29 d_ln_p_demo_55_43_G1_24 d_ln_p_demo_55_43_G1_19      d_ln_p_demo_55_43_G1_sq_32 d_ln_p_demo_55_43_G1_sq_10 d_ln_p_demo_55_43_G1_sq_33 d_ln_p_demo_55_43_G1_sq_31 d_ln_p_demo_55_43_G1_sq_16 d_ln_p_demo_55_43_G1_sq_34 d_ln_p_demo_55_43_G1_sq_12 d_ln_p_demo_55_43_G1_sq_30 d_ln_p_demo_55_43_G1_sq_18 d_ln_p_demo_55_43_G1_sq_23 d_ln_p_demo_55_43_G1_sq_29 d_ln_p_demo_55_43_G1_sq_24 d_ln_p_demo_55_43_G1_sq_19     d_ln_p_demo_55_43_G1_cu_32 d_ln_p_demo_55_43_G1_cu_10 d_ln_p_demo_55_43_G1_cu_33 d_ln_p_demo_55_43_G1_cu_31 d_ln_p_demo_55_43_G1_cu_16 d_ln_p_demo_55_43_G1_cu_34 d_ln_p_demo_55_43_G1_cu_12 d_ln_p_demo_55_43_G1_cu_30 d_ln_p_demo_55_43_G1_cu_18 d_ln_p_demo_55_43_G1_cu_23 d_ln_p_demo_55_43_G1_cu_29 d_ln_p_demo_55_43_G1_cu_24 d_ln_p_demo_55_43_G1_cu_19"

	*pluto G2:
local d_ln_p_pluto_55_43_G2_poly="d_ln_p_pluto_55_43_G1_32 d_ln_p_pluto_55_43_G1_10 d_ln_p_pluto_55_43_G1_33 d_ln_p_pluto_55_43_G1_31 d_ln_p_pluto_55_43_G1_16 d_ln_p_pluto_55_43_G1_34 d_ln_p_pluto_55_43_G1_12 d_ln_p_pluto_55_43_G1_30 d_ln_p_pluto_55_43_G1_18 d_ln_p_pluto_55_43_G1_23 d_ln_p_pluto_55_43_G1_29 d_ln_p_pluto_55_43_G1_24 d_ln_p_pluto_55_43_G1_19      d_ln_p_pluto_55_43_G1_sq_32 d_ln_p_pluto_55_43_G1_sq_10 d_ln_p_pluto_55_43_G1_sq_33 d_ln_p_pluto_55_43_G1_sq_31 d_ln_p_pluto_55_43_G1_sq_16 d_ln_p_pluto_55_43_G1_sq_34 d_ln_p_pluto_55_43_G1_sq_12 d_ln_p_pluto_55_43_G1_sq_30 d_ln_p_pluto_55_43_G1_sq_18 d_ln_p_pluto_55_43_G1_sq_23 d_ln_p_pluto_55_43_G1_sq_29 d_ln_p_pluto_55_43_G1_sq_24 d_ln_p_pluto_55_43_G1_sq_19     d_ln_p_pluto_55_43_G1_cu_32 d_ln_p_pluto_55_43_G1_cu_10 d_ln_p_pluto_55_43_G1_cu_33 d_ln_p_pluto_55_43_G1_cu_31 d_ln_p_pluto_55_43_G1_cu_16 d_ln_p_pluto_55_43_G1_cu_34 d_ln_p_pluto_55_43_G1_cu_12 d_ln_p_pluto_55_43_G1_cu_30 d_ln_p_pluto_55_43_G1_cu_18 d_ln_p_pluto_55_43_G1_cu_23 d_ln_p_pluto_55_43_G1_cu_29 d_ln_p_pluto_55_43_G1_cu_24 d_ln_p_pluto_55_43_G1_cu_19"
}

merge m:1 state43 using ${dump}/QS_V`foldno'/fake_prices_`r'.dta, nogen



#delimit ;


local test="
[eq1_mean]dlog_p_misc_fake=
[eq1_mean]dlog_p_cloth_fake=
[eq2_mean]dlog_p_misc_fake=
[eq2_mean]dlog_p_cloth_fake=
[eq3_mean]dlog_p_misc_fake=
[eq3_mean]dlog_p_cloth_fake=
[eq4_mean]dlog_p_misc_fake=
[eq4_mean]dlog_p_cloth_fake=
[eq5_mean]dlog_p_misc_fake=
[eq5_mean]dlog_p_cloth_fake=
[eq6_mean]dlog_p_misc_fake=
[eq6_mean]dlog_p_cloth_fake=
[eq7_mean]dlog_p_misc_fake=
[eq7_mean]dlog_p_cloth_fake=
[eq8_mean]dlog_p_misc_fake=
[eq8_mean]dlog_p_cloth_fake=
[eq9_mean]dlog_p_misc_fake=
[eq9_mean]dlog_p_cloth_fake=
[eq10_mean]dlog_p_misc_fake=
[eq10_mean]dlog_p_cloth_fake=
[eq11_mean]dlog_p_misc_fake=
[eq11_mean]dlog_p_cloth_fake=
[eq12_mean]dlog_p_misc_fake=
[eq12_mean]dlog_p_cloth_fake=
[eq13_mean]dlog_p_misc_fake=
[eq13_mean]dlog_p_cloth_fake=
[eq14_mean]dlog_p_misc_fake=
[eq14_mean]dlog_p_cloth_fake=
[eq15_mean]dlog_p_misc_fake=
[eq15_mean]dlog_p_cloth_fake=
[eq16_mean]dlog_p_misc_fake=
[eq16_mean]dlog_p_cloth_fake=
[eq17_mean]dlog_p_misc_fake=
[eq17_mean]dlog_p_cloth_fake=
[eq18_mean]dlog_p_misc_fake=
[eq18_mean]dlog_p_cloth_fake=
[eq19_mean]dlog_p_misc_fake=
[eq19_mean]dlog_p_cloth_fake=
[eq20_mean]dlog_p_misc_fake=
[eq20_mean]dlog_p_cloth_fake=
[eq21_mean]dlog_p_misc_fake=
[eq21_mean]dlog_p_cloth_fake=
[eq22_mean]dlog_p_misc_fake=
[eq22_mean]dlog_p_cloth_fake=
[eq23_mean]dlog_p_misc_fake=
[eq23_mean]dlog_p_cloth_fake=
[eq24_mean]dlog_p_misc_fake=
[eq24_mean]dlog_p_cloth_fake=
[eq25_mean]dlog_p_misc_fake=
[eq25_mean]dlog_p_cloth_fake=
[eq26_mean]dlog_p_misc_fake=
[eq26_mean]dlog_p_cloth_fake=
[eq27_mean]dlog_p_misc_fake=
[eq27_mean]dlog_p_cloth_fake=
[eq28_mean]dlog_p_misc_fake=
[eq28_mean]dlog_p_cloth_fake=
[eq29_mean]dlog_p_misc_fake=
[eq29_mean]dlog_p_cloth_fake=
[eq30_mean]dlog_p_misc_fake=
[eq30_mean]dlog_p_cloth_fake=
[eq31_mean]dlog_p_misc_fake=
[eq31_mean]dlog_p_cloth_fake=";

local eq=1;
*These are 31 goods K-1 from each G group: (exclude good 21, 19 and 13 grom G1, G2 and G3 respectively);
foreach i of numlist 25 26 3 2 15 5 22 17 9 27 1 6 20 28 11     {;
reg dlog_rshare_`i' ev cv dlogexp_cap `d_ln_p_demo_55_43_G1_linear'  dlog_p_misc_fake dlog_p_cloth_fake [weight=r4355total_wt] if decile_dist==50;
est store eq`eq';
local eq=`eq'+1;
};
foreach i of numlist  32 10 33 31 16 34 12 30 18 23 29 24    {;
reg dlog_rshare_`i'  ev cv dlogexp_cap  `d_ln_p_demo_55_43_G2_linear'  dlog_p_misc_fake dlog_p_cloth_fake [weight=r4355total_wt] if decile_dist==50;
est store eq`eq';
local eq=`eq'+1;
};
foreach i of numlist  8 4 14 7  {;
reg dlog_rshare_`i'  ev cv dlogexp_cap  `d_ln_p_demo_55_43_G3_linear'  dlog_p_misc_fake dlog_p_cloth_fake [weight=r4355total_wt] if decile_dist==50;
est store eq`eq';
local eq=`eq'+1;
};



suest eq1 eq2 eq3 eq4 eq5 eq6 eq7 eq8 eq9 eq10 eq11 eq12 eq13 eq14 eq15 eq16 eq17 eq18 eq19 eq20 eq21 eq22 eq23 eq24 eq25 eq26 eq27 eq28 eq29 eq30 eq31, vce(cluster market_id) ;
test `test'=0  ;
#delimit cr;

local type "joint, normal"
post `permutation_results_V`foldno'' ("`type'") (`r') (r(chi2)) (r(df)) (r(p)) (e(N))

}



#delimit ;

local test="
[eq1_mean]dlog_p_miscellaneous=
[eq1_mean]dlog_p_clothing=
[eq2_mean]dlog_p_miscellaneous=
[eq2_mean]dlog_p_clothing=
[eq3_mean]dlog_p_miscellaneous=
[eq3_mean]dlog_p_clothing=
[eq4_mean]dlog_p_miscellaneous=
[eq4_mean]dlog_p_clothing=
[eq5_mean]dlog_p_miscellaneous=
[eq5_mean]dlog_p_clothing=
[eq6_mean]dlog_p_miscellaneous=
[eq6_mean]dlog_p_clothing=
[eq7_mean]dlog_p_miscellaneous=
[eq7_mean]dlog_p_clothing=
[eq8_mean]dlog_p_miscellaneous=
[eq8_mean]dlog_p_clothing=
[eq9_mean]dlog_p_miscellaneous=
[eq9_mean]dlog_p_clothing=
[eq10_mean]dlog_p_miscellaneous=
[eq10_mean]dlog_p_clothing=
[eq11_mean]dlog_p_miscellaneous=
[eq11_mean]dlog_p_clothing=
[eq12_mean]dlog_p_miscellaneous=
[eq12_mean]dlog_p_clothing=
[eq13_mean]dlog_p_miscellaneous=
[eq13_mean]dlog_p_clothing=
[eq14_mean]dlog_p_miscellaneous=
[eq14_mean]dlog_p_clothing=
[eq15_mean]dlog_p_miscellaneous=
[eq15_mean]dlog_p_clothing=
[eq16_mean]dlog_p_miscellaneous=
[eq16_mean]dlog_p_clothing=
[eq17_mean]dlog_p_miscellaneous=
[eq17_mean]dlog_p_clothing=
[eq18_mean]dlog_p_miscellaneous=
[eq18_mean]dlog_p_clothing=
[eq19_mean]dlog_p_miscellaneous=
[eq19_mean]dlog_p_clothing=
[eq20_mean]dlog_p_miscellaneous=
[eq20_mean]dlog_p_clothing=
[eq21_mean]dlog_p_miscellaneous=
[eq21_mean]dlog_p_clothing=
[eq22_mean]dlog_p_miscellaneous=
[eq22_mean]dlog_p_clothing=
[eq23_mean]dlog_p_miscellaneous=
[eq23_mean]dlog_p_clothing=
[eq24_mean]dlog_p_miscellaneous=
[eq24_mean]dlog_p_clothing=
[eq25_mean]dlog_p_miscellaneous=
[eq25_mean]dlog_p_clothing=
[eq26_mean]dlog_p_miscellaneous=
[eq26_mean]dlog_p_clothing=
[eq27_mean]dlog_p_miscellaneous=
[eq27_mean]dlog_p_clothing=
[eq28_mean]dlog_p_miscellaneous=
[eq28_mean]dlog_p_clothing=
[eq29_mean]dlog_p_miscellaneous=
[eq29_mean]dlog_p_clothing=
[eq30_mean]dlog_p_miscellaneous=
[eq30_mean]dlog_p_clothing=
[eq31_mean]dlog_p_miscellaneous=
[eq31_mean]dlog_p_clothing=";

local eq=1;
*These are 31 goods K-1 from each G group: (exclude good 21, 19 and 13 grom G1, G2 and G3 respectively);
foreach i of numlist 25 26 3 2 15 5 22 17 9 27 1 6 20 28 11    {;
reg dlog_rshare_`i'  ev cv dlogexp_cap  `d_ln_p_demo_55_43_G1_linear'   dlog_p_miscellaneous dlog_p_clothing [weight=r4355total_wt] if decile_dist==50;
est store eq`eq';
local eq=`eq'+1;
};

foreach i of numlist  32 10 33 31 16 34 12 30 18 23 29 24  {;
reg dlog_rshare_`i'  ev cv dlogexp_cap  `d_ln_p_demo_55_43_G2_linear'   dlog_p_miscellaneous dlog_p_clothing [weight=r4355total_wt] if decile_dist==50;
est store eq`eq';
local eq=`eq'+1;
};

foreach i of numlist   8 4 14 7  {;
reg dlog_rshare_`i'   ev cv dlogexp_cap  `d_ln_p_demo_55_43_G3_linear' dlog_p_miscellaneous dlog_p_clothing [weight=r4355total_wt] if decile_dist==50;
est store eq`eq';
local eq=`eq'+1;
};

suest eq1 eq2 eq3 eq4 eq5 eq6 eq7 eq8 eq9 eq10 eq11 eq12 eq13 eq14 eq15 eq16 eq17 eq18 eq19 eq20 eq21 eq22 eq23 eq24 eq25 eq26 eq27 eq28 eq29 eq30 eq31, vce(cluster market_id) ;
test `test'=0  ;
#delimit cr;

*Note that run "0" indicates the real data regression:
local type "joint, normal"
post `permutation_results_V`foldno'' ("`type'") (0) (r(chi2)) (r(df)) (r(p)) (e(N))


postclose `permutation_results_V`foldno''
*End of permutation regressions:
}

use "$output/permutation_resultsV`foldno'.dta",clear
keep if type=="joint, normal"
sort run
local vertical=round(chi2[_n==1],.01)
local runs=500
sort p
cap drop p_value
gen p_value=(_n/(`runs'+1))
local low=round(chi2[25],.01)
display `low'
local high=round(chi2[475],.01)
display `high'
histogram chi2, xtitle("Test statistic", size(medlarge)) ytitle("Density") xline(`low' `high', lcolor(gray)) xline(`vertical', lcolor(red) lwidth(thick))  fcolor(eltblue%40 erose%25 none none) lcolor(eltblue erose edkblue maroon)  graphregion(lcolor(white) fcolor(white)) 
graph save  "$output/QS_Test_Panel_A.gph", replace

graph export "$master_file_path\output\QS_Test_Panel_A.pdf", replace as(pdf)
cap log close
log using "$output\p-value_result",replace
sort chi2 
display "P-value of joint permutation test"
sum p_value if run==0
log close

*Cleanup:
cap erase "$output/QSV4.dta"
cap erase "$output/QSV4_wide.dta"
cap erase "$output/QSV5_wide.dta"
cap erase "$output/QSV5_wide_4permutation.dta"
cap erase "$output/outside_prices_for_QS.dta"
cap erase "$output/permutation_resultsVPanel_A.dta"


























